package pw_reader

import (
	"bufio"
	"fmt"
	"github.com/spf13/viper"
	"io"
	"os"
	"strings"
)

func Search(keyword string) {
	res := viper.Get("reader-path")
	file, err := os.OpenFile(res.(string), os.O_RDWR, 0666)
	if err != nil {
		fmt.Println("Open file error!", err)
		return
	}
	defer file.Close()

	buf := bufio.NewReader(file)
	count := 0
	for {
		line, err := buf.ReadString('\n') //以'\n'为结束符读入一行
		if err != nil || io.EOF == err {
			break
		}
		count++
		if line == "" {
			break
		}
		if line != "" && strings.Contains(line, keyword) {
			fmt.Println(count, "=>", line)
		}

	}
}
